﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;


public partial class _Default : System.Web.UI.Page 
{
    private decimal orderTotal = 0.0m;//创建一个变量来保存订货金额总和
    public SqlDb sd = new SqlDb();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            DbBind();
    }
    public void DbBind()
    {
        string strCon = ConfigurationSettings.AppSettings["ConnectionString"];
        string strsql = "SELECT 产品编号, 产品, 单价, 库存量, 已订购量, 订单日期,单价 * 已订购量 AS 订货金额 FROM tb_OrderForm";
        sd.BindData(GridView1, strsql);
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        // 提取当前的数据行。
        GridViewRow row = e.Row;
        // 如果正被创建的数据行是一个脚注，则更新数据列加总。
        if (row.RowType == DataControlRowType.Footer)
        {
            // 取得脚注当中的标签控件 OrderTotalTotal 。
            Label total = (Label)(e.Row.FindControl("OrderTotalLabel"));

            // 以货币格式来显示订货金额加总。
            if (total != null)
            {
                total.Text = orderTotal.ToString("c");
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            decimal totalMoney =
               Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "订货金额"));
            //累加订货金额并赋给变量 orderTotal。
            orderTotal += totalMoney;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
        DbBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sqlstr = "select 产品编号, 产品, 单价, 库存量, 已订购量, 订单日期,单价 * 已订购量 AS 订货金额 from tb_OrderForm where 产品 like '%" + tbSearch.Text + "%'";
        sd.BindData(GridView1, sqlstr);
    }
    // strKey为原始内容，keyworld为搜索的关键字//查询的条件高亮显示
    protected string HightLightText(string strKey, string keyworld)
    {
        if (keyworld == "")//如果查询关键字为空返回原内容
        {
            return strKey;
        }
        string strReturn = strKey.Replace(keyworld, "<span class='highlightTxtSearch'>" + keyworld + "</span>");
        return strReturn;
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        DbBind();
    }
}
